02 / 02

How to use worker threads?

javascript
Difficulty: 6/10
Topics: worker_threads API, thread communication, performance

Scenario Questions

0-2 years experience
  1. 1

    We need to offload a CPU‑intensive image resize operation. How would you set up a Worker thread in Node.js to handle a single image file?

  2. 2

    If you spawn a Worker and forget to call worker.terminate(), what will happen when the main process exits?

2-5 years experience
  1. 1

    Your new feature processes user‑uploaded CSV files in parallel using Worker threads, but you notice occasional 'ERR_WORKER_NOT_RUNNING' errors. Walk me through how you'd debug this.

  2. 2

    When deciding between a Worker thread pool and spawning a new Worker per request, what trade‑offs would you consider for a service handling 200 requests per second?

  3. 3

    Explain how you would pass large JSON data to a Worker without copying it unnecessarily.

5-8 years experience
  1. 1

    Our microservice uses Worker threads for heavy calculations, but under load memory usage spikes. How would you redesign the threading model to keep memory bounded?

  2. 2

    Design a strategy to gracefully shut down a Node.js server that has multiple active Workers processing jobs, ensuring no data loss.

  3. 3

    What are the implications of using SharedArrayBuffer vs message passing for coordinating state between Workers in a high‑throughput system?

8+ years experience
  1. 1

    We plan to migrate a legacy monolith that currently uses child_process for parallel work to Worker threads across several teams. What architectural considerations and migration steps would you propose?

  2. 2

    How would you evaluate whether introducing Worker threads at the platform level is worth the operational complexity compared to moving compute‑heavy parts to a separate service written in another language?

  3. 3

    Discuss the long‑term maintenance challenges of a codebase that heavily relies on Worker threads for concurrency, especially regarding debugging, monitoring, and onboarding new engineers.

Follow-up Questions

  • How do you propagate errors from a Worker back to the main thread?
  • What metrics would you collect to monitor Worker performance and health?
  • How would you test the correctness and performance of your Worker implementation?